package com.liuyk.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.liuyk.main.activity.R;

/**
 * DESC
 * <p>
 * Created by liuyakui on 2020/4/16.
 */
public class HandlerActivity extends BaseActivity {
    private TextView handler_text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_handler);
        Log.i(TAG, "MyThread: " + Thread.currentThread().getName() + ": " + Thread.currentThread().getId());
        handler_text = findViewById(R.id.handler_text);
        handler_text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startHandler3();
            }
        });
    }

    class MyHandler extends Handler {

        //注意：Only the original thread that created a view hierarchy can touch its views.
        //这个异常是发生在ViewRootImpl.checkThread(), 根据源码观察，绘制完成发生在onResume之后
        //所以在onResume()之前可以在子线程里面更新UI
        //另外，也不是所有的UI更新，都必须在子线程里面的，只有会触发view绘制流程的方法被调用才会检查线程
        //setText(),就可以在子线程里面调用，但是requestLayout()，就不能在子线程里面调用

        public MyHandler() {

        }

        public MyHandler(Looper looper) {
            super(looper);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Log.i(TAG, "MyThread: " + Thread.currentThread().getName() + ": " + Thread.currentThread().getId());
            handler_text.setText(Thread.currentThread().getName() + ": " + Thread.currentThread().getId());
            handler_text.requestLayout();
        }
    }

    private void startHandler() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Log.i(TAG, "MyThread: " + Thread.currentThread().getName() + ": " + Thread.currentThread().getId());
                Looper.prepare();
                MyHandler handler = new MyHandler();
                handler.sendEmptyMessage(0);
                Looper.loop();
            }
        }).start();
    }

    private void startHandler2() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Log.i(TAG, "MyThread: " + Thread.currentThread().getName() + ": " + Thread.currentThread().getId());
                Looper.prepare();
                MyHandler handler = new MyHandler();
                handler.post(new Runnable() {
                    @Override
                    public void run() {

                    }
                });
                Looper.loop();
            }
        }).start();
    }

    private void startHandler3() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Log.i(TAG, "MyThread: " + Thread.currentThread().getName() + ": " + Thread.currentThread().getId());
                MyHandler handler = new MyHandler(Looper.getMainLooper());
                handler.sendMessage(new Message());
            }
        }).start();
    }
}
